// pages/endetail/endetail.js
const app = getApp()
var url = app.globalData.url
var engid=''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    names: "维修师详情",
    flag: true,
    eng: {},
    comment: {},
    score: 0,
    isGuanzhu:0
  },
  back() {
    wx.navigateBack(1)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options.mid)
    engid=options.mid
    this.getDetails(options.mid)
    this.isGuanzhu()
  },
  getDetails(mid) {
    const that=this
    wx.request({
      url: url + '/engineer/engineer_details',
      data: {
        number: "eaq165"
      },
      header: {
        "content-type": "application/json"
      },
      success(res) {
        console.log(res.data)
        if (res.data.code === 1) {
          that.setData({
            eng: res.data.data.engineer,
            comment: res.data.data.comment,
            score: res.data.data.score
          })
        }
      }
    })
  },
 isGuanzhu() {
   const that = this
   var user = wx.getStorageSync('user')
    wx.request({
      url: url + '/wx_users_engineer/follow_tf',
      data: {
        wxUsersAccount: user.wxUsersAccount,//用户平台账号
        engineerNumber: engid //维修师平台账号
      },
      header: {
        "content-type": "application/json"
      },
      success(res) {
        console.log("是否关注"+res.data)
        if (res.data.code === 1) {
          that.setData({
            isGuanzhu: res.data.data.tf
          })
        }
      }
    })
  },
  guanzhu(){
    const that=this
    var user=wx.getStorageSync('user')
    wx.request({
      url: url+'/wx_users_engineer/add_follow',
      data:{
        wxUsersAccount: user.wxUsersAccount ,//用户平台账号
        engineerNumber: engid //维修师平台账号
      },
      header:{
        "content-type": "application/json"
      },
      success(res){
        if(res.data.code===0){
             wx.showToast({
               title: '关注成功',
               icon:'loading',
               duration:1000
             })
             that.setData({
               isGuanzhu:1
             })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    var ctx = wx.createCanvasContext("randerCarves");
    const that = this
    that.hexagon({
      element: 'randerCarves',
      x: 100,
      y: 100,
      maxR: 62,
      color: '#fff',
      width: '1',
      arc: [{
        r: '30'
      }, {
        r: '42'
      }, {
        r: '52'
      }, {
        r: '62'
      }],
      standard: ["上门准点率", "维修用时", "返修率", "完成率", "服务态度", "等级"]
    }, ctx);
  

  },
  hexagon: function(config, context) {
    //  var context = wx.createCanvasContext(config.element);
    var x = config.x; //圆心的x坐标
    var y = config.y; //圆心的y坐标
    var maxR = config.maxR; //最大半径
    var standard = config.standard; //标准线
    var angle = 360 / standard.length * 2 * Math.PI / 360;
    context.beginPath()
    context.setStrokeStyle(config.color || '#fff');
    context.setLineWidth(config.width || '1');
    var arc = config.arc || [];
    //绘制六边形
    for (let i = 0; i < arc.length; i++) {
      let r = arc[i].r;
      context.moveTo(x + r * Math.cos(angle), y + r * Math.sin(angle));
      for (let i = 1; i < standard.length; i++) {
        context.lineTo(x + r * Math.cos(angle * (i + 1)), y + r * Math.sin(angle * (i + 1)));
      }
      context.lineTo(x + r * Math.cos(angle), y + r * Math.sin(angle));

      //  if (i == 2) {
      context.setStrokeStyle("#999999")
      context.stroke()
      //}
    }

    //绘制标准线
    for (let i = 0; i < standard.length; i++) {
      context.moveTo(x, y); //中心点
      let lineX = x + maxR * Math.cos(angle * (i + 1));
      let lineY = y + maxR * Math.sin(angle * (i + 1));
      context.lineTo(lineX, lineY); //六角点连线
      context.setFontSize("10")

      //绘制文字
      if (lineX < x) {
        context.fillText(standard[i], lineX - 45, lineY + 5);
      } else {
        context.fillText(standard[i], lineX + 8, lineY + 5);
      }

    }
    context.stroke();
    // context.closePath()
    // context.stroke();
    // context.draw();
    // context.beginPath()
    // 绘制连线
    context.beginPath();

    context.setLineWidth('3')
    context.setStrokeStyle("#3192F1")
    var r = 20
    for (var i = 0; i < 6; i++) {
      var mr;
      if (i % 2 == 0) {
        mr = r + 10
        r = mr
      }

      var liex = x - r * Math.cos(angle * (i + 1));
      var liey = y + r * Math.sin(angle * (i + 1));
      console.log("连线")
      context.lineTo(liex, liey)
      // context.lineTo(150 - 30*Math.cos(angle * 1), 150 +30*Math.sin(angle*1));
      // context.lineTo(150 - 30*Math.cos(angle * 2), 150 + 42*Math.sin(angle * 2));
      // context.lineTo(150 - 42 * Math.cos(angle * 3), 150 + 52 * Math.sin(angle * 3));
      // context.lineTo(150 -52 * Math.cos(angle * 4), 150 +62 * Math.sin(angle * 4));
      // context.lineTo(150 - 52 * Math.cos(angle * 5), 150 + 40 * Math.sin(angle * 5));
      // context.lineTo(150 - 52 * Math.cos(angle * 6), 150 + 40 * Math.sin(angle * 6));

    }
    context.setFillStyle("#AD78FC")
    context.setGlobalAlpha(0.5)
    context.fill()
    context.closePath()
    context.stroke();
    context.draw();

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})